<?php ob_start();
session_start(); // on relaye la session 
if(isset($_SESSION["is_registered"]) && $_SESSION["is_registered"] == "registered"){
		$enregistrer = $_SESSION["is_registered"];
}else{
	header("location:../../../menu.php"); // redirection en cas d'echec
}
require_once('../../../Connections/microassur.php');
require_once('../../../utils/fonctions.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])&& isset($_GET['Identifiant_agent'])) {

  $editFormAction .= "?Identifiant_agent=" . htmlentities($_GET['Identifiant_agent']);
}

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1"))
$bool=verifLibelle1('Code_agent') && verifLibelle('Nom_agent') && verifLibelle('Prenom_agent');

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") && $bool && !existeChamp1($database_microassur, $microassur,'agent','Code_agent',@$_POST['Identifiant_agent'],@$_POST['Code_agent'])) {
  $updateSQL = sprintf("UPDATE agent SET Code_agent=%s, Nom_agent=%s, Prenom_agent=%s,Identifiant_unite=%s WHERE Identifiant_agent=%s",
                       GetSQLValueString($_POST['Code_agent'], "text"),
                       GetSQLValueString($_POST['Nom_agent'], "text"),
                       GetSQLValueString($_POST['Prenom_agent'], "text"),
                       GetSQLValueString($_POST['Identifiant_unite'], "int"),
                       GetSQLValueString($_POST['Identifiant_agent'], "int"));

  mysql_select_db($database_microassur, $microassur);
  mysql_query("START TRANSACTION");
  $Result1 = mysql_query($updateSQL, $microassur) or die(mysql_error());

 if($Result1){ mysql_query("commit");

	//$updateGoTo = "index.php?mod=success";
 
 // header(sprintf("Location: %s", $updateGoTo));
     $message=colorer("La modification de l'agent est prise en compte","true");

  }
   else {mysql_query("rollback");$message=colorer("Contacter la DSI, erreur modification agent","false") ;}

}
elseif(isset($_POST["MM_update"]) && !$bool)
{
if(empty($_POST['Code_agent'])) $message=colorer("Vous devez saisir le code","false");
elseif(!verifLibelle1('Code_agent')) $message=colorer("Le code saisi comporte des caractères spéciaux interdits","false");

elseif(empty($_POST['Nom_agent'])) $message=colorer("Vous devez saisir le nom ","false");
elseif(!verifLibelle('Nom_agent')) $message=colorer("Le nom saisi comporte des caractères spéciaux interdits ","false");
elseif(!verifLibelle('Prenom_agent')) $message=colorer("Vous devez saisir le prénom ","false");
}

elseif ((isset($_POST["MM_update"])) && existeChamp1($database_microassur, $microassur,'agent','Code_agent',@$_POST['Identifiant_agent'],@$_POST['Code_agent'])) $message=colorer("L'agent existe déjà","false");
$colname_rs = "-1";
if (isset($_GET['Identifiant_agent'])) {
  $colname_rs = $_GET['Identifiant_agent'];
}

mysql_select_db($database_microassur, $microassur);
$query_rs = sprintf("SELECT * FROM agent WHERE Identifiant_agent = %s", GetSQLValueString($colname_rs, "int"));
$rs = mysql_query($query_rs, $microassur) or die(mysql_error());
$row_rs = mysql_fetch_assoc($rs);
$totalRows_rs = mysql_num_rows($rs);

mysql_select_db($database_microassur, $microassur);
$query_rs_unite = "SELECT * FROM unite";
$rs_unite = mysql_query($query_rs_unite, $microassur) or die(mysql_error());
$row_rs_unite = mysql_fetch_assoc($rs_unite);
$totalRows_rs_unite = mysql_num_rows($rs_unite);
?>
<?php  if(isset($_GET['supp']) && $_GET['supp']=='err') $message=colorer("Contacter la DSI, erreur suppression agent","false"); ?>
<?php if(isset($_GET['supp']) && $_GET['supp']=='success') $message=colorer("L'agent est supprimé","true") ;?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<meta http-equiv="X-UA-Compatible" content="IE=8" />
			<title><?php echo $titleGen ?></title>
			<link rel="stylesheet" type="text/css" href="../../../css/main.css" />
            <link rel="stylesheet" type="text/css" href="list.css" />
			<!--[if IE 7]>
            	<link rel="stylesheet" type="text/css" href="../../../css/ie7.css" />
            	<link rel="stylesheet" type="text/css" href="form_ie7.css" />
            <![endif]-->
	</head>
<body>
			<div id="sf_menu1">
				<h3>Administration</h3>
				<?php
					if(isset($message))
						echo $message;
					$nommodule="Agent";
					$nommodule2="Détail agent";
					?>
			</div>
			<div class="cleared"></div>
			<!-- /*******************************/ -->
			<div class="corps1">
				<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
							<div class="imgleft"></div>
							<div class="imgmiddle">
								<div class="btn"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule; ?></h5></span><span class="btnright"></span></div>
								<div class="btn1"><span class="btnleft"></span><span class="btnmiddle"><h5><?php echo $nommodule2; ?></h5></span><span class="btnright"></span></div>
									<div class="formulaire">
											<table class="tabform">
												<tr>
													<td>CODE</td>
												  <td><input class="resize_1" type="text" maxlength="10" name="Code_agent" value="<?php if(isset($_POST['Code_agent']))echo remind_text('Code_agent'); else echo htmlentities($row_rs['Code_agent'], ENT_COMPAT, 'utf-8'); ?>" size="32" /></td>
												</tr>
												<tr>
												  <td>
												  <p>NOM</p></td>
												  <td><input name="Nom_agent" type="text"  maxlength="30"value="<?php if(isset($_POST['Nom_agent']))echo remind_text('Nom_agent'); else echo htmlentities($row_rs['Nom_agent'], ENT_COMPAT, 'utf-8'); ?>" size="40" />												  </td>
												</tr>
												<tr>
												  <td>
												  <p>PRENOM</p></td>
												  <td><input name="Prenom_agent" type="text" maxlength="30" value="<?php if(isset($_POST['Prenom_agent']))echo remind_text('Prenom_agent'); else echo htmlentities($row_rs['Prenom_agent'], ENT_COMPAT, 'utf-8'); ?>" size="40" />												  </td>
												</tr>
												<tr>
												  <td>UNITE</td>
												  <td><select name="Identifiant_unite">
													<?php
											do {  
											?>
													<option value="<?php echo $row_rs_unite['Identifiant_unite']?>"<?php if (!(strcmp($row_rs_unite['Identifiant_unite'], htmlentities($row_rs['Identifiant_unite'], ENT_COMPAT, 'utf-8')))) {echo "selected=\"selected\"";} ?>><?php echo $row_rs_unite['Nom_unite']?></option><?php
											} while ($row_rs_unite = mysql_fetch_assoc($rs_unite));
											  $rows = mysql_num_rows($rs_unite);
											  if($rows > 0) {
												  mysql_data_seek($rs_unite, 0);
												  $row_rs_unite = mysql_fetch_assoc($rs_unite);
											  }
											?>
												  </select>												  </td>
												</tr>
											  </table>
							  </div>
							</div>
							<div class="imgright"></div>				
							<div class="cleared"></div>
				
								<div class="bt1"><a href="./index.php"><span class="btleft"></span><span class="btmiddle">Retour</span><span class="btright"></span></a></div>
								<div class="bt"><a <?php if(isset($row_rs['Identifiant_agent'])){ ?> href="suppagent.php?Identifiant_agent=<?php echo $row_rs['Identifiant_agent']; ?>"<?php } else echo 'href="#"';?>><span class="btleft"></span><span class="btmiddle">Supprimer</span><span class="btright"></span></a></div>
								<div class="bt"><span class="btleft"></span><input <?php if(retourValForm('Identifiant_agent')==NULL || retourValForm('supp')=='success') echo "disabled='disabled'"; ?> type="submit" name="Modifier" value="Modifier" class="resizeall btmiddle"/><span class="btright"></span></div>
								
							<div class="cleared"></div>
							
					  <input type="hidden" name="Identifiant_agent" value="<?php echo $row_rs['Identifiant_agent']; ?>" />
					  <input type="hidden" name="MM_update" value="form1" />
				</form>
		</div> 
</body>
</html>
<?php
mysql_free_result($rs);

mysql_free_result($rs_unite);
?>
<?php ob_end_flush();?>